import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/login.vue'
import home from '../components/home.vue'
import welcome from '../components/welcome.vue'
import User from '../components/user/User.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import categories from '../components/goods/categories.vue'
import params from '../components/goods/params.vue'
import goodList from '../components/goods/goodList.vue'
import add from '../components/goods/add.vue'
import orderList from '../components/orders/orderList.vue'
import report from '../components/report/report.vue'
Vue.use(VueRouter)
const routes = [{
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/home',
    component: home,
    redirect: '/welcome',
    children: [{
        path: '/welcome',
        component: welcome
      },
      {
        path: '/users',
        component: User
      },
      {
        path: '/Rights',
        component: Rights
      },
      {
        path: '/Roles',
        component: Roles
      },
      {
        path: '/categories',
        component: categories
      },
      {
        path: '/params',
        component: params
      },
      {
        path: '/goods',
        component: goodList
      },
      {
        path: '/goods/add',
        component: add
      },
      {
        path: '/orders',
        component: orderList
      },
      {
        path: '/reports',
        component: report
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  // to 表示将要访问的路径
  // from 代表从哪个路径跳转过来
  // next next()跳转 next("路径") 强制跳转
  if (to.path === '/login') next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
